//
//  HRHearRootTitleTypeView.m
//  Hear
//
//  Created by mac on 2018/1/9.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRHearRootTitleTypeView.h"
#import "HRMainRootModel.h"
@interface HRHearRootTitleTypeView()
@property (nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic, strong)UIButton *tempButton;

@end
@implementation HRHearRootTitleTypeView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.scrollView];
        if (@available(iOS 11.0, *)) {
            self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.scrollView.contentInset = UIEdgeInsetsMake(-frame.size.height, 0, 0, 0);
        }

    }
    return self;
}

-(void)setTitleArray:(NSArray *)titleArray{
    _titleArray = titleArray;
    CGFloat buttonWidth = kScreenWidth/4;
    self.scrollView.contentSize = CGSizeMake(buttonWidth*titleArray.count, self.scrollView.height);
    for (int i=0; i <titleArray.count; i++) {
        HRHearTitleTypeModel *model = titleArray[i];
        UIButton *button = [UIButton buttonWithType:0];
        [button setTitle:model.Name forState:0];
        [button setTitleColor:FirstTextColor forState:0];
        button.titleLabel.font = CustomUIFont(15);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
        button.tag = i;
        button.frame = CGRectMake(i*buttonWidth, 5, buttonWidth, self.height-10);
        button.backgroundColor = [UIColor whiteColor];
        if (i == 0){
            [button setTitleColor:BlueColor forState:0];
            [button setBackgroundImage:[UIImage imageNamed:@"圆角矩形89"] forState:0];
            self.tempButton = button;
        }
    }
}

- (void)buttonClick:(UIButton *)button{
    [self.tempButton setTitleColor:FirstTextColor forState:0];
    [self.tempButton setBackgroundImage:nil forState:0];
    self.tempButton = button;
    HRHearTitleTypeModel *model = self.titleArray[button.tag];

    [button setBackgroundImage:[UIImage imageNamed:@"圆角矩形89"] forState:0];
    [button setTitleColor:BlueColor forState:0];
    if (self.selectTypeCodeBlock){
        self.selectTypeCodeBlock(model.Code);
    }
}

@end
